package cn.iocoder.yudao.module.member.service.user;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.alipay.api.FileItem;
import com.alipay.api.response.AlipayTradeRefundResponse;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipayTradePagePay {

    public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        // 设置订单绝对超时时间
        model.setTimeExpire("2016-12-31 10:05:01");

        // 设置业务扩展参数
        ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId("2088511833207846");
        extendParams.setHbFqSellerPercent("100");
        extendParams.setHbFqNum("3");
        extendParams.setTcInstallmentOrderId("2015042321001004720200028594");
        extendParams.setIndustryRefluxInfo("{\"scene_code\":\"metro_tradeorder\",\"channel\":\"xxxx\",\"scene_data\":{\"asset_name\":\"ALIPAY\"}}");
        extendParams.setSpecifiedSellerName("XXX的跨境小铺");
        extendParams.setRoyaltyFreeze("true");
        extendParams.setCardType("S0JP0000");
        extendParams.setTradeComponentOrderId("2023060801502300000008810000005657");
        model.setExtendParams(extendParams);

        // 设置返回参数选项
        List<String> queryOptions = new ArrayList<String>();
        queryOptions.add("hyb_amount");
        queryOptions.add("enterprise_pay_info");
        model.setQueryOptions(queryOptions);

        // 设置描述结算信息
        SettleInfo settleInfo = new SettleInfo();
        settleInfo.setSettlePeriodTime("7d");
        List<SettleDetailInfo> settleDetailInfos = new ArrayList<SettleDetailInfo>();
        SettleDetailInfo settleDetailInfos0 = new SettleDetailInfo();
        settleDetailInfos0.setAmount("0.1");
        settleDetailInfos0.setTransIn("A0001");
        settleDetailInfos0.setSettleEntityType("SecondMerchant");
        settleDetailInfos0.setSummaryDimension("A0001");
        settleDetailInfos0.setActualAmount("0.1");
        settleDetailInfos0.setSettleEntityId("2088xxxxx;ST_0001");
        settleDetailInfos0.setTransInType("cardAliasNo");
        settleDetailInfos.add(settleDetailInfos0);
        settleInfo.setSettleDetailInfos(settleDetailInfos);
        model.setSettleInfo(settleInfo);

        // 设置订单标题
        model.setSubject("Iphone6 16G");

        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        // 设置订单附加信息
        model.setBody("Iphone6 16G");

        // 设置PC扫码支付的方式
        model.setQrPayMode("1");

        // 设置请求后页面的集成方式
        model.setIntegrationType("PCWEB");

        // 设置商户的原始订单号
        model.setMerchantOrderNo("20161008001");

        // 设置二级商户信息
        SubMerchant subMerchantKMqWi = new SubMerchant();
        subMerchantKMqWi.setMerchantId("2088000603999128");
        subMerchantKMqWi.setMerchantType("alipay");
        model.setSubMerchant(subMerchantKMqWi);

        // 设置开票信息
        InvoiceInfo invoiceInfo = new InvoiceInfo();
        InvoiceKeyInfo keyInfo = new InvoiceKeyInfo();
        keyInfo.setTaxNum("1464888883494");
        keyInfo.setIsSupportInvoice(true);
        keyInfo.setInvoiceMerchantName("ABC|003");
        invoiceInfo.setKeyInfo(keyInfo);
        invoiceInfo.setDetails("[{\"code\":\"100294400\",\"name\":\"服饰\",\"num\":\"2\",\"sumPrice\":\"200.00\",\"taxRate\":\"6%\"}]");
        model.setInvoiceInfo(invoiceInfo);

        // 设置外部指定买家
        ExtUserInfo extUserInfo = new ExtUserInfo();
        extUserInfo.setCertType("IDENTITY_CARD");
        extUserInfo.setCertNo("362334768769238881");
        extUserInfo.setName("李明");
        extUserInfo.setMobile("16587658765");
        extUserInfo.setMinAge("18");
        extUserInfo.setNeedCheckInfo("F");
        extUserInfo.setIdentityHash("27bfcd1dee4f22c8fe8a2374af9b660419d1361b1c207e9b41a754a113f38fcc");
        model.setExtUserInfo(extUserInfo);

        // 设置建议使用time_expire字段
        model.setTimeoutExpress("90m");

        // 设置禁用渠道
        model.setDisablePayChannels("pcredit,moneyFund,debitCardExpress");

        // 设置签约参数
        AgreementSignParams agreementSignParams = new AgreementSignParams();
        SignMerchantParams subMerchanttIZaX = new SignMerchantParams();
        subMerchanttIZaX.setSubMerchantName("滴滴出行");
        subMerchanttIZaX.setSubMerchantServiceName("滴滴出行免密支付");
        subMerchanttIZaX.setSubMerchantServiceDescription("免密付车费，单次最高500");
        subMerchanttIZaX.setSubMerchantId("2088123412341234");
        agreementSignParams.setSubMerchant(subMerchanttIZaX);
        agreementSignParams.setBuckleAppId("1001164");
        agreementSignParams.setSignValidityPeriod("2m");
        agreementSignParams.setBuckleMerchantId("268820000000414397785");
        agreementSignParams.setExternalLogonId("138****8888");
        agreementSignParams.setThirdPartyType("PARTNER");
        agreementSignParams.setPersonalProductCode("GENERAL_WITHHOLDING_P");
        agreementSignParams.setExternalAgreementNo("test");
        agreementSignParams.setPromoParams("{\"key\",\"value\"}");
        agreementSignParams.setSignScene("INDUSTRY|CARRENTAL");
        model.setAgreementSignParams(agreementSignParams);

        // 设置描述分账信息
        RoyaltyInfo royaltyInfo = new RoyaltyInfo();
        royaltyInfo.setRoyaltyType("ROYALTY");
        List<RoyaltyDetailInfos> royaltyDetailInfos = new ArrayList<RoyaltyDetailInfos>();
        RoyaltyDetailInfos royaltyDetailInfos0 = new RoyaltyDetailInfos();
        royaltyDetailInfos0.setOutRelationId("20131124001");
        royaltyDetailInfos0.setAmountPercentage("100");
        royaltyDetailInfos0.setAmount("0.1");
        royaltyDetailInfos0.setBatchNo("123");
        royaltyDetailInfos0.setTransIn("2088101126708402");
        royaltyDetailInfos0.setTransOutType("userId");
        royaltyDetailInfos0.setTransOut("2088101126765726");
        royaltyDetailInfos0.setSerialNo(1L);
        royaltyDetailInfos0.setTransInType("userId");
        royaltyDetailInfos0.setDesc("分账测试1");
        royaltyDetailInfos.add(royaltyDetailInfos0);
        royaltyInfo.setRoyaltyDetailInfos(royaltyDetailInfos);
        model.setRoyaltyInfo(royaltyInfo);

        // 设置商户门店编号
        model.setStoreId("NJ_001");

        // 设置请求来源地址
        model.setRequestFromUrl("https://");

        // 设置商户自定义二维码宽度
        model.setQrcodeWidth(100L);

        // 设置订单包含的商品列表信息
        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
        GoodsDetail goodsDetail0 = new GoodsDetail();
        goodsDetail0.setOutSkuId("outSku_01");
        goodsDetail0.setGoodsName("ipad");
        goodsDetail0.setAlipayGoodsId("20010001");
        goodsDetail0.setQuantity(1L);
        goodsDetail0.setPrice("2000");
        goodsDetail0.setOutItemId("outItem_01");
        goodsDetail0.setGoodsId("apple-01");
        goodsDetail0.setGoodsCategory("34543238");
        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
        goodsDetail.add(goodsDetail0);
        model.setGoodsDetail(goodsDetail);

        // 设置指定支付渠道
        model.setEnablePayChannels("pcredit,moneyFund,debitCardExpress");

        // 设置商户订单号
        model.setOutTradeNo("20150320010101001");

        // 设置订单总金额
        model.setTotalAmount("88.88");

        // 设置商户传入业务信息
        model.setBusinessParams("{\"mc_create_trade_ip\":\"127.0.0.1\"}");

        // 设置优惠参数
        model.setPromoParams("{\"storeIdType\":\"1\"}");

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiV03uYYHPdn1+okE+xBcnaDmExoblB1pkNulCZrGui0mwWqILhL8QPdNWMzQlisI/dXcmv7GSwMVe9NQoyUlHxVu602+QG1vPZGEJ+DiPGT2/BleR73wWEPHxFHyZqUtgT+VSsOXvOx8G3oadZ5gIYN4csiwptrQ9YdNslNbr3akr8gkjOwroAEvD0KLyNUNjge+u28jxHTzC5fBqb9591zVMWZLgG6b0NfOYtGRX/+9wa0TIp63c+B5m7tgCw733CItxJi1IqpPSuJbXpYFxAFL+odhuJPRVoWEwC+sPRMyGh00txBU3CT3QEl70ddbOY/5werJ0h7ZYbNA4E7zTAgMBAAECggEBAI41DiZQsgbLVBlXBvJ1kZbKnQArbEU6ULtSwriQ/vrYNv/OFP8kFL6kefmbtwVf7NLsEikWaPYV+Y85xpPH53KAdt9MkgDZhm5BDyRNlTX9NXKdmp5S/A7UXywA9zkf7NB8jHUIUxNiZcZ9rcw60BoeyNK4XSlZS8AtbqhCJZbM1sYykk0jzczFkmPfDWIbEX040pWvXtI5XxKLg6B3BP9rM2OVwWtJeuIaZpgL9HbH/iF5Wr1zsUzZc6T5fd+lVLOQTL7xS/cU4szdA6Q2S00D+9FSBQu1CJlv7/ghKzIDSjogXe4b8iPZN5W6Q/DiGnbrMWR0JztD6jRTRLBFa8ECgYEA7zBQsL7+5hMCOtv6W3yIIJLjqJ6B2sgnMhxeov0V1awH1viib6aU2yxztY1QlCino6q2mZCwPamNb5g5L3hV+sa6wA/x+tkD7F+1bSpP2O92x9CV+0wqEI8GmLsXtX7gOi0lkaby/dXHvrjOyKtlsWuyxVnROMrUHqWUJdz/aOECgYEArcBH4uxdqgf3/elp4zFytDvNrPWhvCyAORJL7nWBNzT9yjgUtKPaV+nbc6WcJhxtQ4C2/bqH6f0r2Z5e9U0j4oPn0cKBDoY1/f2dcOdDx2TKs+Qrxb2mxPpujxxgCnlCusCcPemUkvEQgD3ZtD8ObXyaGJY2IBDvwSH6KcM02DMCgYEA5d4R/fpgL65e+0ADV77TeDKSla9XbnHisVbwYEVHoaSFmEQ0n4aBEi2jOfyITwrPKQxcFjMmHOyAn5m0g3lrb+li+keYrxG2oojMbBA40mw4RAmiyfJFjspVdZyASY2YYnCW5WaDqgy0vQcQu2t0/NBa6yzJVIIg3x8TxFuiHiECgYAmohZZ0oyQ/eyaPIgayCflA1ccGhG4MXzhM7xGbUIp8FJt4EJaWAR4f7sX3770imWRGNGXYR5PXMefP6OFj31nJEUrUsA3ZogizX1wWTus3jBkB1vkP+MQ8U4b7Ac+zO+VbrZO3dz3PzqYCSia91pR5d6tkNjAEIP+wXtKRek+GQKBgHmG9fuVfbdzhy0phtYTjnDKoQxwwI503449twctANvpuyQZ3Hkpweyxhojilsg3hifn4Qv5jcFw2WKtziI8LyX9UqkSUKID5ec+SV+gjECFVBxNCVU7KdTAEXChYtUFbB7qMa/smst5grxjTJ8ods/4Cp0ipqOeOpkTfk6FvOie";
        String alipayPublicKey ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxfHsbr3YdENUK4JUk8x2jHBfkpX9FIjjS+DybDt/KzYjeDSojuxedqR6tVu1iua3OHMSAsX50cjO6gPsAWCvj8/s9l6ijWUMgdCPsPcpPWtr3lDMjJCRTAUIVgTU5aPwbt7cI5OI1YguojjpjcHbf1gBntX7/Im5yK2cbWv3FSSqKfsCovSx1qwwHxiWTZ1Q2Ri4FMwMu0aAHLRskJbMga1Ah2bdC/aoNVkYm6K6c9tmrUAhfUcSuieDh6MsqnYkNNDqAV2pSDl/WjIoAdj3LHHjv/Omfbk54d9noIXY1v5yc5ApiqA/jwUntPvvQCWi6SYHkecAqQFcuEg9WR5d5QIDAQAB";

        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021004150671568");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
